package com.inossem.warehousestorage_inossem.activity.in_storage;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.ListView;
import android.widget.TextView;

import com.inossem.loadandrefresh.PullToRefreshLayout;
import com.inossem.loadandrefresh.pullableview.PullableListView;
import com.inossem.utils.base.QRCodeUtils;
import com.inossem.utils.base.Utils;
import com.inossem.utils.constant.QRCodeConstant;
import com.inossem.utils.listener.NoDoubleClickListener;
import com.inossem.warehousestorage_inossem.R;
import com.inossem.warehousestorage_inossem.activity.base.BaseActivity;
import com.inossem.warehousestorage_inossem.adapter.inStorage.ReceiveInStorageListAdapter;
import com.inossem.warehousestorage_inossem.bean.inStorage.InStorageReceiveListBean;
import com.inossem.warehousestorage_inossem.listener.GsonBeanListener;
import com.inossem.warehousestorage_inossem.request.inStorage.InStorageReceiveListRequest;
import com.inossem.warehousestorage_inossem.utils.ActivityUtils;

import java.util.List;

//验收入库
public class InStorageReceiveListActivity extends BaseActivity {

    private TextView title;

    private PullToRefreshLayout refresh;

    private PullableListView receiveInStorageList;

    private List<InStorageReceiveListBean.DataBean.ReceiveInStorageListBean> allData;

    private ReceiveInStorageListAdapter adapter;

    private String mInStorageNumber;

    @Override
    protected void onCreateImpl(Bundle savedInstanceState) {
        setContentView(R.layout.activity_in_storage_receive_list);
        initView(InStorageReceiveListActivity.this);
        initData(InStorageReceiveListActivity.this);
    }

    @Override
    protected void onResumeImpl() {

    }

    private void initView(Activity activity) {
        title = (TextView) activity.findViewById(R.id.headerTitle);
        receiveInStorageList = (PullableListView) activity.findViewById(R.id.receiveInStorageList);
        refresh = (PullToRefreshLayout) activity.findViewById(R.id.receiveInStorageListRefresh);
    }

    private void initData(Activity activity) {
        title.setText(activity.getResources().getString(R.string.activity_in_storage_manage_receive_title));
        ActivityUtils.initBackAndFunction(activity, R.string.activity_in_storage_manage_title, new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View v) {
                finish();
            }
        });

        receiveInStorageList.setPullUpFlag(false);
        refresh.setOnRefreshListener(new PullToRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh(PullToRefreshLayout pullToRefreshLayout) {
                initData(activity);
                pullToRefreshLayout.refreshFinish(PullToRefreshLayout.SUCCEED);
            }

            @Override
            public void onLoadMore(PullToRefreshLayout pullToRefreshLayout) {
            }
        });
        setmScanListener((String scanResult, int length) -> {
            getData(activity, QRCodeUtils.DecryptQRCode(scanResult));
        }, QRCodeConstant.TYPE_PURCHASE_ORDER);
        getData(activity, mInStorageNumber);
    }

    private void addInStorageReceiveListData(Context context, List<InStorageReceiveListBean.DataBean.ReceiveInStorageListBean> receiveInStorageList, ListView listView, boolean flag) {
        if (flag) {
            adapter = new ReceiveInStorageListAdapter(context, receiveInStorageList, mInStorageNumber, (String inStorageNumber) -> {
                mInStorageNumber = inStorageNumber;
                getData(context, mInStorageNumber);
            });
            listView.setAdapter(adapter);
        }
    }

    private void getData(Context context, String condition) {
        InStorageReceiveListRequest.request(context, condition, new GsonBeanListener<InStorageReceiveListBean>() {
            @Override
            public void onSuccess(InStorageReceiveListBean bean) {
                allData = bean.getData().getReceiveInStorageList();
                if (allData==null || allData.isEmpty()) {
                    Utils.middleToast(context, context.getResources().getString(R.string.activity_in_storage_receive_list_no_message));
                }
                mInStorageNumber = condition;
                addInStorageReceiveListData(context, ActivityUtils.getShowData(allData, 0), receiveInStorageList, true);
            }
        });
    }



    public interface InStorageReceiveListener {
        void onNoDoubleClickListener(String inStorageNumber);
    }

}
